package com.calvin.study.aqs;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

public class SimpleMockLock implements Lock {

	private final Sync sync = new Sync();

	//自定义同步器
	private static class Sync extends AbstractQueuedSynchronizer {

		// 钩子方法
		protected boolean tryAcquire(int arg) {
			// CAS更新状态值为1
			if (compareAndSetState(0, 1)) {
				setExclusiveOwnerThread(Thread.currentThread());
				return true;
			}
			return false;
		}

		// 钩子方法
		protected boolean tryRelease(int arg) {
			// 如果当前线程不是占用锁的线程
			if (Thread.currentThread() != getExclusiveOwnerThread()) {
				// 抛出非法状态的异常
				throw new IllegalMonitorStateException();
			} // 如果锁的状态为没有占用
			if (getState() == 0) {
				// 抛出非法状态的异常
				throw new IllegalMonitorStateException();
			}
			// 接下来不需要使用CAS操作， 因为下面的操作不存在并发场景
			setExclusiveOwnerThread(null);
			// 设置状态
			setState(0);
			return true;
		}

	}

	//显式锁的抢占方法
	@Override
	public void lock() {
		//委托给同步器的acquire()抢占方法
		sync.acquire(1);
	}

	@Override
	public void lockInterruptibly() throws InterruptedException {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean tryLock() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void unlock() {
		//委托给同步器的release()释放方法
		sync.release(1);
	}

	@Override
	public Condition newCondition() {
		// TODO Auto-generated method stub
		return null;
	}

}
